package com.moogene.android.engine.mmi;

import java.lang.ref.SoftReference;
import java.util.HashMap;

public class LayerCacher {

	private final static HashMap<String, SoftReference<Layer>> softCacheLayers = new HashMap<String, SoftReference<Layer>>();


	public static Layer getLayer(String name){
		Layer l = null;
		if (softCacheLayers.get(name) != null) {
			l = softCacheLayers.get(name).get();
		}
		if (l == null) {
			try {
				l = (Layer)Class.forName(com.moogene.android.Config.UI_OBJECT_CLASS_ROOT_PATH + name).newInstance();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				throw new IllegalArgumentException("No Class found under " + com.moogene.android.Config.UI_OBJECT_CLASS_ROOT_PATH + name);
			}
		}
		softCacheLayers.put(name, new SoftReference<Layer>(l));
		return l;
	}
}
